package com.lifesaftypower.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.lifesaftypower.entity.Product;

public class ProductDetailActivity extends FragmentActivity {

	public static final String ARGS_MODEL = "ARGS_MODEL";
	SectionsPagerAdapter _sectionsPagerAdapter;
	Product _product;
	ViewPager _viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle extra = getIntent().getExtras();

		_product = Product.getByModel(extra.getString(ARGS_MODEL));
		setContentView(R.layout.activity_product_detail);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		_sectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		_viewPager = (ViewPager) findViewById(R.id.pager);
		_viewPager.setAdapter(_sectionsPagerAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_product_detail, menu);
		return true;
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a DummySectionFragment (defined as a static inner class
			// below) with the page number as its lone argument.
			Fragment fragment = new WebViewSectionFragment();
			
			Bundle args = new Bundle();
			switch (position) {
			case 0:
				args.putString(WebViewSectionFragment.ARG_HTML_CONTENT, _product.getSpec());	
				break;
			case 1:
				args.putString(WebViewSectionFragment.ARG_HTML_CONTENT, _product.getOverview());
				break;
			case 2:
				args.putString(WebViewSectionFragment.ARG_HTML_CONTENT, _product.getDocument());	
				break;
			case 3:
				args.putString(WebViewSectionFragment.ARG_HTML_CONTENT, _product.getSupport());	
				break;
			}
					
			fragment.setArguments(args);
			
			return fragment;
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return 4;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case 0:
				return getString(R.string.title_product).toUpperCase();
			case 1:
				return getString(R.string.title_overview).toUpperCase();
			case 2:
				return getString(R.string.title_document).toUpperCase();
			case 3:
				return getString(R.string.title_support).toUpperCase();
			}
			return null;
		}
	}
	
	public static class WebViewSectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_HTML_CONTENT = "section_number";

		public WebViewSectionFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			
			Bundle extra = this.getArguments();

			String htmlContent = extra.getString(ARG_HTML_CONTENT);
			View v = inflater.inflate(R.layout.web_content, null);
			WebView webView = (WebView)v.findViewById(R.id.wvContent);
			webView.loadData(htmlContent, "text/html", "utf-8");
			return v;
		}
	}

}
